package persist

import (
	"context"
	"errors"
	"log"
	"spider/engine"

	"github.com/elastic"
)

func ItemSaver(index string) (chan engine.Item, error) {

	client, err := elastic.NewClient(
		elastic.SetSniff(false))
	if err != nil {
		return nil, err
	}

	out := make(chan engine.Item)
	go func() {
		itemCount := 0
		for {
			item := <-out
			log.Printf("ItemSaver: Got Item #%d: %v\n", itemCount, item)
			itemCount += 1

			err := save(client, index, item)
			if err != nil {
				log.Printf("Item Saver: error saveing item : %v %v\n", item, err)
				continue
			}

		}
	}()

	return out, nil
}

func save(client *elastic.Client, index string, item engine.Item) error {

	if item.Type == "" {
		return errors.New("must suppyly Type")
	}

	indexService := client.Index().Index(index).Type(item.Type).
		Id(item.Id).BodyJson(item)

	if item.Id != "" {
		indexService.Id(item.Id)
	}

	_, err := indexService.Do(context.Background())
	return err
}
